<template>
  <div>
    <h1>springBoot+mysql+vue</h1>
    <router-link to="/add">新增</router-link>
    <table>
      <tr>
        <th>id</th>
        <th>userName</th>
        <th>userAge</th>
        <th>userAddress</th>
        <th>编辑</th>
        <th>删除</th>
      </tr>
      <tr v-for="item in userData" :key="item.id">
        <td>{{item.id}}</td>
        <td>{{item.userName}}</td>
        <td>{{item.userAge}}</td>
        <td>{{item.userAddress}}</td>
        <td>
          <router-link :to="{ path: '/edit', query: {id: item.id} }">编辑</router-link>
        </td>
        <td @click="deleted(item.id)">删除</td>
      </tr>
    </table>
  </div>
</template>

<script>

import axios from '@/service/httpService'
export default {
  data () {
    return {
      userData: ''
    }
  },
  methods: {
    getData () {
      axios
        .get('/users')
        .then(response => {
          this.userData = response.data
          console.log(response)
        })
        .catch(error => {
          console.log(error)
        })
    },
    deleted (id) {
      var deleteConfirm = confirm('是否删除')
      if (deleteConfirm) {
        axios.delete(`/deleteUser/${id}`).then(response => {
          this.getData()
        })
      }
    }
  },
  created () {
    this.getData()
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
table {
  border-collapse: collapse;
}
td,
th {
  border: 1px solid #000;
}
</style>
